import { execute } from "../../database/connector";

import { ProjectChangeQueries } from "../queries/project-change.query";
import { IProjectChange } from "../../models/project-change.model";

/**
 * gets active projectChanges
 */
export const getProjectChanges = async () => {
  return execute<IProjectChange[]>(ProjectChangeQueries.GetProjectChanges, []);
};

/**
 * gets done projectChanges
 */
export const getDoneProjectChanges = async () => {
    return execute<IProjectChange[]>(ProjectChangeQueries.GetDoneProjectChanges, []);
  };

/**
 * gets in progress projectChanges
 */
export const getInProgressProjectChanges = async () => {
    return execute<IProjectChange[]>(ProjectChangeQueries.GetInProgressProjectChanges, []);
};

/**
 * gets in progress projectChanges by project id
 */
export const GetInProgressProjectChangesByPId = async (id: IProjectChange['id']) => {
    return execute<IProjectChange[]>(ProjectChangeQueries.GetInProgressProjectChangesByPId, [id]);
};

/**
 * gets a projectChange based on id provided
 */
export const getProjectChangeById = async (id: IProjectChange['id']) => {
  return execute<IProjectChange>(ProjectChangeQueries.GetProjectChangeById, [id]);
};

/**
 * gets a projectChange based on project id provided
 */
export const getProjectChangesByPId = async (pId: IProjectChange['pId']) => {
  return execute<IProjectChange[]>(ProjectChangeQueries.GetProjectChangeByPId, [pId]);
};

/**
 * gets projectChange list based on project id provided
 */
export const getProjectChangesByProjects = async (idList: number[]) => {
  return execute<IProjectChange[]>(ProjectChangeQueries.GetProjectChangeByProjects, [
    Object.values(idList)
  ]);
};

/**
 * gets projectChange list based on group id provided
 */
export const getDoneProjectChangesByGroups = async (idList: number[]) => {
    return execute<IProjectChange[]>(ProjectChangeQueries.GetDoneProjectChangesByGroups, [
      [Object.values(idList)]
    ]);
  };

/**
 * adds a new active projectChange record
 */
export const insertProjectChange = async (projectChange: IProjectChange) => {
  const result = await execute<{ affectedRows: number }>(ProjectChangeQueries.AddProjectChange, [
    [Object.values(projectChange)]
  ]);
  return result.affectedRows > 0;
};

/**
 * updates projectChange information based on the id provided
 */
export const updateProjectChange = async (projectChange: IProjectChange) => {
  const result = await execute<{ affectedRows: number }>(ProjectChangeQueries.UpdateProjectChangeById, [projectChange, projectChange.id]);
  return result.affectedRows > 0;
};

/**
 * updates projectChange information based on the id provided
 */
 export const deleteProjectChange = async (id: IProjectChange['id']) => {
  const result = await execute<{ affectedRows: number }>(ProjectChangeQueries.DeleteProjectChangeById, [
    id
  ]);
  return result.affectedRows > 0;
};